<?php
set_include_path($_SERVER['DOCUMENT_ROOT']);
include_once 'admin/includes/functions/admin-functions.php';
ini_set('display_errors',1);
ini_set('error_reporting',E_ALL ^ E_NOTICE ^ E_WARNING);
global  $CONFIG,$SMARTY,$DBobject;

echo "=> Cron started<br>";
$classTermObj = new ClassTermClass();
$enrol_obj = new Enrolment();

$devAdmin = "readysteadygokids@bccgmail.com";
$from = (string) $CONFIG->company->name;
$fromEmail = 'noreply@'. str_replace('www.', '', $GLOBALS['HTTP_HOST']);
$COMP = json_encode($CONFIG->company);
$SMARTY->assign('COMPANY', json_decode($COMP,TRUE));
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$domain = $protocol . $GLOBALS['HTTP_HOST'];
$SMARTY->assign('DOMAIN', $domain);

$events = array();
$membersEmailArr = array();

$subject = "澳睿跑 (Ready Steady Go Kids) | 生日派对";
$day = date('m-d', strtotime('+49 days'));

//Only at THEM
if($_SERVER['REMOTE_ADDR']=='150.101.230.130' && !empty($_REQUEST['date'])){
	$day = date('m-d', strtotime($_REQUEST['date']));
}

echo "=> Birthday party: $day  <br>";

//Get "Birthday party " message
$sql = "SELECT email_additional_content FROM tbl_email_additional WHERE email_additional_deleted IS NULL AND email_additional_id = '16' "; 
$res = $DBobject->wrappedSql($sql);
$email_content = unclean($res[0]['email_additional_content']);

if(empty($email_content)) sendMail($devAdmin, $from, $fromEmail, "CRON JOB ERROR", "Content message is null.");

try {
	// Get current terms
	$sql = "SELECT * FROM tbl_term WHERE term_deleted IS NULL AND term_published = 1 AND CURDATE() <= DATE(term_end_date) ORDER BY term_start_date"; 
	if($terms = $DBobject->wrappedSql($sql)){
		$termArr = array();
		foreach($terms as $t){
			$termArr[] = "enrolment_term_object_id = '{$t['term_object_id']}'";
		}
		$termSQL = (empty($termArr))?'':' AND ('.implode(' OR ', $termArr) . ')';
					
		$sql = "SELECT user_id, user_gname, user_surname, user_email, student_name, student_surname 
			FROM tbl_student 
				LEFT JOIN tbl_user ON user_id = student_user_id 
			WHERE student_deleted IS NULL AND user_deleted IS NULL 
				AND	DATE_FORMAT(student_dob,'%m-%d') = :student_dob 
				AND EXISTS (SELECT enrolment_student_id FROM tbl_enrolment WHERE enrolment_deleted IS NULL AND enrolment_published = '1'  
					AND enrolment_student_id = student_id $termSQL)
			GROUP BY user_email";
		if($memberArr = $DBobject->wrappedSql($sql, array(':student_dob'=>$day))){
			$membersIdsArr = array();
			foreach($memberArr as $e){
				$membersEmailArr[] = $e['user_email'];
				$membersIdsArr[] = $e['user_id'];
			}
			//$SMARTY->assign('content', $email_content);
			//$body = $SMARTY->fetch('cron/email-bday.tpl');
			$body = $email_content;
			createBulkMail($membersEmailArr, $from, $fromEmail, $subject, $body, 0, $membersIdsArr);
			//echo "<br>=> Emails sent: ". sendBulkMail(); // "cron_email_queue.php" will send them
			$events['records'] = $membersEmailArr; 
		} 
	}
} catch (Exception $e) {
	$events['error'][] = "Error while running".$e;
}

//sendMail($devAdmin, $from, $fromEmail, "CRON JOB - PARTY $day", json_encode($events));
createBulkMail(array($devAdmin), $from, $fromEmail, "CRON JOB - PARTY $day", json_encode($events));
echo "<br>=> Cron ended";

